package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.WelfareTypeMapper;
import com.qztc.hire_end_dto.dto.WelfareType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/welfareType")
@Tag(name = "福利类型管理接口")
public class WelfareTypeController {
    @Autowired
    private WelfareTypeMapper welfaretypeMapper;

    @Operation(summary = "查询所有福利类型")
    @GetMapping("/getAll")
    public List<WelfareType> getAll() {
        return new WelfareType().selectAll();
    }

    @Operation(summary = "根据welfareTypeId获取具体福利类型")
    @GetMapping("/getbywelfareTypeId")
    public WelfareType getbywelfareTypeId(@RequestParam Integer welfareTypeId) {
        QueryWrapper<WelfareType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("welfare_typeid",welfareTypeId);
        return new WelfareType().selectOne(queryWrapper);
    }

    @Operation(summary = "根据welfareTypeId删除某一个具体福利类型")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody WelfareType welfareType) {
        return welfareType.deleteById(welfareType.getWelfareTypeId());
    }

    @Operation(summary = "根据welfareTypeId修改某一个具体福利类型")
    @PutMapping("/update")
    public boolean update(@RequestBody WelfareType welfareType) {
        QueryWrapper<WelfareType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("welfare_typename",welfareType.getWelfareTypeName());
        if(welfareType.selectCount(queryWrapper) > 0){
            return false;
        }
        return welfareType.updateById();
    }

    @Operation(summary = "增加福利类型")
    @PostMapping("/add")
    public boolean add(@RequestBody WelfareType welfareType) {
        QueryWrapper<WelfareType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("welfare_typename",welfareType.getWelfareTypeName());
        if(welfareType.selectCount(queryWrapper) > 0){
            return false;
        }
        return welfareType.insert();
    }

    @Operation(summary = "批量删除福利类型")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> welfareTypeIds) {
        if (welfareTypeIds == null || welfareTypeIds.isEmpty()) {
            return false;
        }
        QueryWrapper<WelfareType> wrapper = new QueryWrapper<>();
        wrapper.in("welfare_typeid", welfareTypeIds);
        return new WelfareType().delete(wrapper);
    }

    @Operation(summary = "批量增加福利类型")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<WelfareType> welfareTypes) {
        if (welfareTypes == null || welfareTypes.isEmpty()) {
            return false;
        }
        // 批量重复校验
        QueryWrapper<WelfareType> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("welfare_typename",
                welfareTypes.stream().map(WelfareType::getWelfareTypeName).collect(Collectors.toList()));
        if (welfaretypeMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        // 批量插入SQL
        String sql = "insert into welfaretype (welfare_typeid, welfare_typename) values" +
                welfareTypes.stream()
                        .map(type -> String.format("(%d,'%s')", type.getWelfareTypeId(), type.getWelfareTypeName()))
                        .collect(Collectors.joining(","));
        return welfaretypeMapper.executeSql(sql);
    }
}